package com.gzdh.tang.dishrecognition.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.util.ObjectUtil;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.dishrecognition.model.KlStateRes;
import com.gzdh.tang.mobile.entity.Kl;
import com.gzdh.tang.mobile.service.KlService;
import com.gzdh.tang.modu.entity.*;
import com.gzdh.tang.modu.service.*;
import com.gzdh.tang.modu.util.AESUtil;
import com.gzdh.tang.modu.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("dr/sysSet")
@RestController
public class DrSysSetController {


    @Autowired
    MacMainpSbruanDinnerService macMainpSbruanDinnerService;

    @Autowired
    MacMainpSbruanService macMainpSbruanService;

    @Autowired
    MacMainpSbruanKlService macMainpSbruanKlService;

    @Autowired
    KlService klService;


    @Autowired
    FkService fkService;

    @Autowired
    GsService gsService;

    @GetMapping("getSbruanDinner")
    public Result<Object> getSbruanDinner(@RequestParam("deviceId") String deviceId) {
        Result<Object> result = new Result<>();

        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>()
                .lambda().eq(MacMainpSbruan::getMac, deviceId));

        List<MacMainpSbruanDinner> dinnerList = macMainpSbruanDinnerService.list(new QueryWrapper<MacMainpSbruanDinner>().lambda()
                .eq(MacMainpSbruanDinner::getDeviceId, macMainpSbruan.getId()));

        result.setResult(dinnerList);
        return result;
    }


    @PutMapping("addSbruanDinner")
    public Result<Object> addSbruanDinner(@RequestParam("deviceId") String deviceId,
                                          @RequestParam("bm") String bm,
                                          @RequestParam("beginTime") String beginTime,
                                          @RequestParam("endTime") String endTime) {
        Result<Object> result = new Result<>();

        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>()
                .lambda().eq(MacMainpSbruan::getMac, deviceId));

        List<MacMainpSbruanDinner> dinnerList = macMainpSbruanDinnerService.list(new QueryWrapper<MacMainpSbruanDinner>().lambda()
                .eq(MacMainpSbruanDinner::getDeviceId, macMainpSbruan.getId()));
        try {
            for (MacMainpSbruanDinner goo : dinnerList) {
                if (DateUtil.hourMinuteBetween(beginTime, goo.getStartTime(), goo.getEndTime())) {

                    return Result.error("时间段重复,添加失败");
                }
                if (DateUtil.hourMinuteBetween(endTime, goo.getStartTime(), goo.getEndTime())) {
                    return Result.error("时间段重复,添加失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

            return Result.error("添加失败#101");
        }

        MacMainpSbruanDinner macMainpSbruanDinner = new MacMainpSbruanDinner();
        macMainpSbruanDinner.setDinner(bm);
        macMainpSbruanDinner.setStartTime(beginTime);
        macMainpSbruanDinner.setEndTime(endTime);
        macMainpSbruanDinner.setDeviceId(macMainpSbruan.getId());
        macMainpSbruanDinner.setDinnerName("");
        macMainpSbruanDinnerService.save(macMainpSbruanDinner);
        dinnerList = macMainpSbruanDinnerService.list(new QueryWrapper<MacMainpSbruanDinner>().lambda()
                .eq(MacMainpSbruanDinner::getDeviceId, macMainpSbruan.getId()));
        result.setResult(dinnerList);
        return result;
    }


    @GetMapping("getKlState")
    public Result<Object> getKlState(@RequestParam("deviceId") String deviceId) {
        Result<Object> result = new Result<>();
        MacMainpSbruan macMainpSbruan = macMainpSbruanService
                .getOne(new QueryWrapper<MacMainpSbruan>().lambda().eq(MacMainpSbruan::getMac, deviceId));
        if (macMainpSbruan == null) {
            return Result.error("机号不存在");
        }
        List<MacMainpSbruanKl> macMainpSbruanKlList = macMainpSbruanKlService.list(new QueryWrapper<MacMainpSbruanKl>()
                .lambda().eq(MacMainpSbruanKl::getDeviceId, macMainpSbruan.getId()));
        List<Kl> klList = klService.list();

        int i = 0;
        List<KlStateRes> klStateResList = new ArrayList<>();
        for (MacMainpSbruanKl goo : macMainpSbruanKlList) {
            Kl kl1 = klList.stream().filter(kl -> kl.getBm().trim().equals(goo.getKl().trim())).findFirst().orElse(null);
            if (kl1 != null) {
                KlStateRes klStateRes = new KlStateRes();
                klStateRes.setId(String.valueOf(goo.getId()));
                klStateRes.setXuhao(++i);
                klStateRes.setBm(goo.getZk());
                klStateRes.setKlName(kl1.getKmc());
                klStateResList.add(klStateRes);
            }
        }
        result.setResult(klStateResList);
        return result;
    }

    @GetMapping("getFk")
    public Result<Object> getFk(@RequestParam("deviceId") String deviceId, @RequestParam("cardsn") String cardSn) {
        Result<Object> result = new Result<>();
        Fk fk = fkService.getOne(new QueryWrapper<Fk>().lambda().eq(Fk::getCardsn, cardSn));
        if (fk == null) {
            return Result.error("查无此人");
        }
        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>().lambda().eq(MacMainpSbruan::getMac, deviceId));

        int count = gsService.count(new QueryWrapper<Gs>().lambda().eq(Gs::getKh, fk.getKh()).eq(Gs::getCardno, fk.getCardno()));
        if (count > 0) {
            return Result.error("此卡已挂失");
        }
        Kl kl = klService.getOne(new QueryWrapper<Kl>().lambda().eq(Kl::getBm, fk.getKl()));
        if (kl != null && kl.getValid() == 1) {
            //判断有效期
            if (fk.getValiddate().compareTo(new Date()) < 0) {
                return Result.error("此卡已超过有效期");
            }
        }
        List<MacMainpSbruanKl> macMainpSbruanKlList = macMainpSbruanKlService.list(new QueryWrapper<MacMainpSbruanKl>().lambda()
                .eq(MacMainpSbruanKl::getDeviceId, macMainpSbruan.getId()));
        if (!judeKl(macMainpSbruanKlList, fk.getKl())) {
            return Result.error("此卡类不许消费");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cardId", ObjectUtil.getStr(fk.getId()));
        jsonObject.put("name", ObjectUtil.getStr(fk.getName()));
        jsonObject.put("kye", fk.getKye());
        jsonObject.put("btye", fk.getBtye());
        result.setResult(jsonObject);
        return result;
    }

    private boolean judeKl(List<MacMainpSbruanKl> macMainpSbruanKlList, String kl) {
        for (MacMainpSbruanKl goo : macMainpSbruanKlList) {
            if (goo.getKl().equals(kl.trim())) {
                return true;
            }
        }
        return false;
    }

    @DeleteMapping("deleteKl")
    public Result<Object> deleteKl(@RequestParam("id") String id, @RequestParam("deviceId") String deviceId) {
        Result<Object> result = new Result<>();
        MacMainpSbruan macMainpSbruan = macMainpSbruanService
                .getOne(new QueryWrapper<MacMainpSbruan>().lambda().eq(MacMainpSbruan::getMac, deviceId));
        if (macMainpSbruan == null) {
            return Result.error("机号不存在");
        }
        AESUtil.decrypt("sdsd","3423432");
        macMainpSbruanKlService.remove(new QueryWrapper<MacMainpSbruanKl>().lambda().eq(MacMainpSbruanKl::getId, id)
                .eq(MacMainpSbruanKl::getDeviceId, macMainpSbruan.getId()));

        List<MacMainpSbruanKl> macMainpSbruanKlList = macMainpSbruanKlService.list(new QueryWrapper<MacMainpSbruanKl>()
                .lambda().eq(MacMainpSbruanKl::getDeviceId, macMainpSbruan.getId()));
        List<Kl> klList = klService.list();

        int i = 0;
        List<KlStateRes> klStateResList = new ArrayList<>();
        for (MacMainpSbruanKl goo : macMainpSbruanKlList) {
            Kl kl1 = klList.stream().filter(kl -> kl.getBm().trim().equals(goo.getKl().trim())).findFirst().orElse(null);
            if (kl1 != null) {
                KlStateRes klStateRes = new KlStateRes();
                klStateRes.setId(String.valueOf(goo.getId()));
                klStateRes.setXuhao(++i);
                klStateRes.setBm(goo.getZk());
                klStateRes.setKlName(kl1.getKmc());
                klStateResList.add(klStateRes);
            }
        }
        result.setResult(klStateResList);
        return result;
    }
}
